﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SCS.Common.Models;
using SCS.Common;

namespace SCS.DAL
{
    public class PrivateMessageRepository : Repository<PrivateMessage>, IPrivateMessageRepository
    {
        public PrivateMessageRepository(SCSContext _context)
            : base(_context)
        { }

        /// <summary>
        /// Get Paged Sent Messages By User
        /// </summary>
        /// <param name="pageIndex">Index of page</param>
        /// <param name="pageSize">Size of page</param>
        /// <param name="user">user has message</param>
        /// <returns>List PrivateMessage</returns>
        public IPagedList<PrivateMessage> GetPagedSentMessagesByUser(int pageIndex, int pageSize, MembershipUser user)
        {
            var totalCount = DBSet.Count(x => x.UserFrom.userID == user.userID && x.isSenderDeleted ==false);

            // Get the topics using an efficient
            var results = DBSet
                                .Where(x => x.UserFrom.userID == user.userID && x.isSenderDeleted == false)
                                .OrderByDescending(x => x.dateSent)
                                .Skip((pageIndex - 1) * pageSize)
                                .Take(pageSize)
                                .ToList();

            // Return a paged list
            return new PagedList<PrivateMessage>(results, pageIndex, pageSize, totalCount);
        }

        /// <summary>
        /// Get Paged Received Messages By User
        /// </summary>
        /// <param name="pageIndex">Index of page</param>
        /// <param name="pageSize">Size of page</param>
        /// <param name="user">user has message</param>
        /// <returns>List PrivateMessage</returns>
        public IPagedList<PrivateMessage> GetPagedReceivedMessagesByUser(int pageIndex, int pageSize, MembershipUser user)
        {
            var totalCount = DBSet.Count(x => x.UserTo.userID == user.userID && x.isReceiverDeleted == false);

            // Get the topics using an efficient
            var results = DBSet
                                .Where(x => x.UserTo.userID == user.userID && x.isReceiverDeleted == false)
                                .OrderByDescending(x => x.dateSent)
                                .Skip((pageIndex - 1) * pageSize)
                                .Take(pageSize)
                                .ToList();


            // Return a paged list
            return new PagedList<PrivateMessage>(results, pageIndex, pageSize, totalCount);
        }

        /// <summary>
        /// Get Last Sent Private Message
        /// </summary>
        /// <param name="Id">Id of user</param>
        /// <returns>Private Message</returns>
        public PrivateMessage GetLastSentPrivateMessage(int Id)
        {
            return DBSet
                                .Where(x => x.UserFrom.userID == Id && x.isSenderDeleted == false)
                                .OrderByDescending(x => x.dateSent)
                                .Take(1)
                                .FirstOrDefault();
        }

        /// <summary>
        /// Get All Message Sent By User
        /// </summary>
        /// <param name="Id">Id of user</param>
        /// <returns>List Private Message</returns>
        public IList<PrivateMessage> GetAllSentByUser(int Id)
        {
            return DBSet
                                .Where(x => x.UserFrom.userID == Id && x.isSenderDeleted == false)
                                .OrderByDescending(x => x.dateSent)
                                .ToList();
        }

        /// <summary>
        /// Get All Message Received By User
        /// </summary>
        /// <param name="Id">Id of user</param>
        /// <returns>List Private Message</returns>
        public IList<PrivateMessage> GetAllReceivedByUser(int Id)
        {
            return DBSet
                                .Where(x => x.UserTo.userID == Id && x.isReceiverDeleted == false)
                                .OrderByDescending(x => x.dateSent)
                                .ToList();
        }
        /// <summary>
        /// Get Last Receive Private Message
        /// </summary>
        /// <param name="userName">UserName</param>
        /// <returns>Private Message</returns>
        public int GetLastReceiceID(string userName)
        {
            return DBSet
                                .Where(x => x.UserTo.userName == userName && x.isSenderDeleted == false)
                                .OrderByDescending(x => x.dateSent)
                                .Take(1)
                                .FirstOrDefault().messageID;
        }

        /// <summary>
        /// Get All Message By User To Another User
        /// </summary>
        /// <param name="senderId">id of sender</param>
        /// <param name="receiverId">id of receiver</param>
        /// <returns>List Private Message</returns>
        public IList<PrivateMessage> GetAllByUserToAnotherUser(int senderId, int receiverId)
        {
            return DBSet
                                .Where(x => x.UserFrom.userID == senderId && x.UserTo.userID == receiverId)
                                .OrderByDescending(x => x.dateSent)
                                .ToList();
        }

        /// <summary>
        /// Count new message
        /// </summary>
        /// <param name="userName">User Name</param>
        /// <returns>Number new message</returns>
        public int NewPrivateMessageCount(string userName)
        {
            return DBSet.Count(x => x.UserTo.userName == userName && !x.isRead && x.isReceiverDeleted == false);
        }


    }
}
